package io.gitee.jingtuo.android.device

import android.content.Context
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.ui.Modifier
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.preferencesDataStore
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import io.gitee.jingtuo.android.device.ui.main.MainPage
import io.gitee.jingtuo.android.device.ui.search.prop.SearchPropPage
import io.gitee.jingtuo.android.device.ui.splash.SplashPage
import io.gitee.jingtuo.android.device.ui.theme.AndroidDeviceDataTheme

val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "settings")

class MainActivity : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            AndroidDeviceDataTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    val navController = rememberNavController()
                    NavHost(navController = navController, startDestination = "splash") {
                        composable("splash") { backStackEntry ->
                            SplashPage() {
                                //初始化完成, 进入主页
                                navController.navigate("main") {
                                    launchSingleTop = true
                                    popUpTo("splash") {
                                        inclusive = true
                                    }
                                }
                            }
                        }
                        composable("main") {
                            MainPage() {
                                navController.navigate("searchProp")
                            }
                        }
                        composable("searchProp") {
                            SearchPropPage()
                        }
                    }
                }
            }
        }
    }
}